﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Net.Http;
using System.Web.Http.Filters;

namespace Freshbox2._0.WEBAPI.Filters
{
    public class LoginCheckAttribute : ActionFilterAttribute
    {
        public bool IsCheck { get; set; }
        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            base.OnActionExecuting(actionContext);
            if (IsCheck)
            {
                var uid = actionContext.Request.Headers.GetValues("uid").FirstOrDefault();
                var token = actionContext.Request.Headers.GetValues("token").FirstOrDefault();

                var tokenInCache = HttpRuntime.Cache.Get(uid);
                if (tokenInCache == null || tokenInCache.ToString() != token)
                {
                    actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, new { tokenState = 0, msg = "token过期或者token不正确" });
                }

            }

        }
    }
}